﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>地图文档要素查询</title>
    <link href="../../css/leaflet/style.css" rel="stylesheet" type="text/css"/>
    <script src="./static/libs/include-leaflet-local.js"></script>
    <style>
        body {
            height: 700px;
        }
    </style>
    <script type="text/javascript">
        //使用严格模式
        "use strict";
        //定义地图文档图层和地图
        var mapDocLayer, map;
        //绘制的查询要素
        var resFeatures = L.featureGroup();
        var { protocol, ip, port } = window.webclient;

        /** 初始化地图显示*/
        function init() {
            //地图容器
            map = L.map('leaf_map', {
                //投影坐标系
                crs: L.CRS.EPSG4326,
                //中心点
                center: [(29.0125822276524 + 33.2932017737021) / 2, (108.34341 + 116.150939561213)
                / 2],
                //最大级数
                maxZoom: 15,
                //最小级数
                minZoom: 0,
                //显示级数
                zoom: 6
            });
            //实例化地图文档图层，作为查询图层
            mapDocLayer = new Zondy.Map.MapDocLayer("Hubei4326", {
                //IP地址
                ip: `${ip}`,
                //端口号
                port: `${port}`,
                //只显示一个图层,不平铺显示
                noWrap: true
            }).addTo(map);
            //设置矩形范围
            var bounds = [[31, 112], [30, 113]];
            //将矩形几何添加到地图进行显示（非必需，仅仅为了在地图上高亮显示图形）
            L.rectangle(bounds, {
                //颜色
                color: "#ff7800",
                //宽度
                weight: 1
            }).addTo(map);
            resFeatures.addTo(map)
        }

        /** 执行文档要素查询*/
        function mapFeatureQuery() {
            //显示进度条
            startPressBar();
            //创建查询结构对象
            var queryStruct = new Zondy.Service.QueryFeatureStruct();
            //是否包含几何图形信息
            queryStruct.IncludeGeometry = true;
            //是否包含属性信息
            queryStruct.IncludeAttribute = true;
            //是否包含图形显示参数
            queryStruct.IncludeWebGraphic = false;
            //创建一个用于查询的矩形
            var geomObj = new Zondy.Object.Rectangle(112, 30, 113, 31);
            //制定查询规则
            var rule = new Zondy.Service.QueryFeatureRule({
                //是否将要素的可见性计算在内
                EnableDisplayCondition: false,
                //是否完全包含
                MustInside: false,
                //是否仅比较要素的外包矩形
                CompareRectOnly: false,
                //是否相交
                Intersect: true
            });
            //实例化查询参数对象
            var queryParam = new Zondy.Service.QueryParameter({
                //几何对象
                geometry: geomObj,
                //结果格式
                resultFormat: "json",
                //查询结构
                struct: queryStruct,
                //查询规则
                rule: rule
            });
            //设置查询分页号
            queryParam.pageIndex = 0;
            //设置查询要素数目
            queryParam.recordNumber = 20;
            //实例化地图文档查询服务对象
            var queryService = new Zondy.Service.QueryDocFeature(queryParam, "Hubei4326", 1, {
                //IP地址
                ip: `${ip}`,
                //端口号
                port: `${port}`
            });
            //执行查询操作，querySuccess为成功回调，queryError为失败回调
            queryService.query(querySuccess, queryError);
        }

        /** 查询成功回调函数
         *  @param {json对象} result 获取结果对象
         */
        function querySuccess(result) {
            //获取查询到的结果数组,ploygonArr的个数即为查询到的结果数
            var ploygonArr = result.SFEleArray;
            for (var i = 0; i < ploygonArr.length; i++) {
                //获取要素几何数组
                var Rings = ploygonArr[i].fGeom.RegGeom[0].Rings;
                //针对复合要素，要循环获取每一个几何
                for (var j = 0; j < Rings.length; j++) {
                    //取出构成多边形的数组
                    var dots = Rings[j].Arcs[0].Dots;
                    //查询结果点集
                    var finaldots = [];
                    for (var k = 0; k < dots.length; k++) {
                        //注意，leaflet是用纬经度来表示位置
                        finaldots.push([dots[k].y, dots[k].x]);
                    }
                    //绘制多边形
                    resFeatures.addLayer(L.polygon(finaldots, {color: 'red', weight: 1}));
                    //清空结果点集，以绘制下一个图形对象
                    finaldots = null;
                    finaldots = [];
                }
            }
            //停止进度条
            stopPressBar();
        }

        /** 查询失败回调函数*/
        function queryError() {
            //停止进度条
            stopPressBar();
            alert("查询失败！");
        }

        /** 删除绘制的要素*/
        function deleteFeatures() {
            resFeatures.clearLayers()
        }

        /** 开始进度条动画*/
        function startPressBar() {
            document.getElementById('preview').style.display = "";
        }

        /** 停止进度条动画*/
        function stopPressBar() {
            document.getElementById('preview').style.display = "none";
        }
    </script>
</head>
<body onload="init();">
<div id="preview" style="display: none; text-align: center; padding-top: 250px; font-weight: bold">
    <img src="./static/assets/graphic-image/39-1.gif" alt=''/><br/>
    <br/>
    <span>正在操作，请稍候</span>
</div>
<div class="ToolLib">
    <input type="button" class="ButtonLib" value="地图文档要素查询" onclick="mapFeatureQuery()"/>
    <input type="button" class="ButtonLib" value="删除查询结果" onclick="deleteFeatures()"/>
</div>
<div id="leaf_map" style="width: 100%; height:95%;position: absolute;">
</div>
</body>
</html>
